package com.nulp.psycho.db.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.nulp.psycho.db.ConnectionManager;
import com.nulp.psycho.db.dao.BaseDao;
import com.nulp.psycho.db.mapping.Converter;
import com.nulp.psycho.db.model.User;
import com.nulp.psycho.hash.MD5;

public class UserDao implements BaseDao{
	
	private final String ADD_USER = "INSERT INTO user (role_id, fname, lname, birthday, sex, login, password, email, confirmation_status, hash) VALUES (?,?,?,?,?,?,?,?,?,?)";
	private final String GET_USER_BY_ID = "SELECT * FROM user WHERE id = ?";
	private final String REMOVE_USER = "DELETE FROM user WHERE id = ?";
	private final String UPDATE_USER = "UPDATE user SET role_id=?, fname=?, lname=?, birthday=?, sex=?, login=?, password=?, email=?, confirmation_status=?, hash=? WHERE id = ?";
	private final String GET_USER_BY_LOGIN_AND_PASSWORD = "SELECT * FROM user WHERE login = ? AND password = ?";
	private final String GET_USER_BY_LOGIN = "SELECT * FROM user WHERE login = ?";
	private final String GET_USER_BY_EMAIL = "SELECT * FROM user WHERE email = ?";
	private final String GET_USER_BY_HASH = "SELECT * FROM user WHERE hash = ?";
	private final String GET_ALL_USERS = "SELECT * FROM user";

	@Override
	public <T> void create(T element) {
		User temp = (User) element;
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(ADD_USER)) {
			preparedStatement.setInt(1, temp.getRoleId());
			preparedStatement.setString(2, temp.getfName());
			preparedStatement.setString(3, temp.getlName());
			preparedStatement.setDate(4, temp.getBirthday());
			preparedStatement.setString(5, temp.getSex());
			preparedStatement.setString(6, temp.getLogin());
			preparedStatement.setString(7, temp.getPassword());
			preparedStatement.setString(8, temp.getEmail());
			preparedStatement.setBoolean(9, temp.getConfirmationStatus());
			preparedStatement.setString(10, MD5.getHash(temp.getLogin() + temp.getEmail()));
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T read(Integer id) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		User user = null;
		try (PreparedStatement statement = connection
				.prepareStatement(GET_USER_BY_ID)) {
			statement.setInt(1, id);
			ResultSet rs = statement.executeQuery();
			if (rs.next())
				user = Converter.<User> fromResaultSetToObject(rs,
						User.class);
		} catch (SQLException e) {
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) user;		
	}

	@Override
	public void delete(Integer id) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(REMOVE_USER)) {
			preparedStatement.setInt(1, id);
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}

	@Override
	public <T> void update(T element) {
		User temp = (User) element;
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(UPDATE_USER)) {
			preparedStatement.setInt(1, temp.getRoleId());
			preparedStatement.setString(2, temp.getfName());
			preparedStatement.setString(3, temp.getlName());
			preparedStatement.setDate(4, temp.getBirthday());
			preparedStatement.setString(5, temp.getSex());
			preparedStatement.setString(6, temp.getLogin());
			preparedStatement.setString(7, temp.getPassword());
			preparedStatement.setString(8, temp.getEmail());
			preparedStatement.setBoolean(9, temp.getConfirmationStatus());
			preparedStatement.setString(10, temp.getHash());
			preparedStatement.setInt(11, temp.getId());
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}

	@SuppressWarnings("unchecked")
	public <T> T getUserByLoginAndPassword(String login, String password) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		User user = null;
		try (PreparedStatement statement = connection
				.prepareStatement(GET_USER_BY_LOGIN_AND_PASSWORD)) {
			statement.setString(1, login);
			statement.setString(2, password);
			ResultSet rs = statement.executeQuery();
			if (rs.next())
				user = Converter.<User> fromResaultSetToObject(rs, User.class);
		} catch (SQLException e) {

		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) user;
	}

	@SuppressWarnings("unchecked")
	public <T> T getUserByLogin(String login) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		User user = null;
		try (PreparedStatement statement = connection.prepareStatement(GET_USER_BY_LOGIN)) {
			statement.setString(1, login);
			ResultSet rs = statement.executeQuery();
			if (rs.next())
				user = Converter.<User> fromResaultSetToObject(rs, User.class);
		} catch (SQLException e) {
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) user;
	}

	@SuppressWarnings("unchecked")
	public <T> T getUserByEmail(String email) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		User user = null;
		try (PreparedStatement statement = connection.prepareStatement(GET_USER_BY_EMAIL)) {
			statement.setString(1, email);
			ResultSet rs = statement.executeQuery();
			if (rs.next())
				user = Converter.<User> fromResaultSetToObject(rs, User.class);
		} catch (SQLException e) {
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) user;
	}

	@SuppressWarnings("unchecked")
	public <T> T getUserByHash(String hash) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		User user = null;
		try (PreparedStatement statement = connection.prepareStatement(GET_USER_BY_HASH)) {
			statement.setString(1, hash);
			ResultSet rs = statement.executeQuery();
			if (rs.next())
				user = Converter.<User> fromResaultSetToObject(rs, User.class);
		} catch (SQLException e) {
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) user;
	}

	@SuppressWarnings("unchecked")
	public <T> T getAllUsers() {
		Connection connection = ConnectionManager.getInstance().getConnection();
		List<User> users = new ArrayList<User>();
		try (PreparedStatement statement = connection.prepareStatement(GET_ALL_USERS)) {
			ResultSet rs = statement.executeQuery();
			while (rs.next()) {
				users.add(Converter.<User> fromResaultSetToObject(rs, User.class));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) users;
	}
	
	
}
